package level.editor.control.collision;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import level.editor.control.IconConstants;
import level.editor.control.layer.LayerBlock;

public class CollisionLayerBlock extends LayerBlock
{
	private JLabel label;
	private JPanel labelPanel;

	public CollisionLayerBlock(CollisionLayer layer)
	{
		super();
		this.layer = layer;
		label = null;

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		setMaximumSize(new Dimension(377, 30));
		setBackground(BACKGROUND);
		setBorder(BorderFactory.createRaisedBevelBorder());
		setAlignmentX(Component.CENTER_ALIGNMENT);
		createNameField();
		createModButtons();
		setFocusable(true);
		addMouseListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Vis"))
		{
			if (e.getSource() instanceof JButton)
			{
				JButton button = (JButton) e.getSource();
				button.setIcon((!layer.isVisible() ? new ImageIcon(
						IconConstants.ICON_DIR + "/eye_open.png")
						: new ImageIcon(IconConstants.ICON_DIR
								+ "/eye_close.png")));
				layer.setVisible(!layer.isVisible());
				layer.getParent().repaint();
			}
		}
		if (e.getActionCommand().equals("Lock"))
		{
			if (e.getSource() instanceof JButton)
			{
				JButton button = (JButton) e.getSource();
				button.setIcon((!layer.isLocked() ? new ImageIcon(
						IconConstants.ICON_DIR + "/lock_close.png")
						: new ImageIcon(IconConstants.ICON_DIR
								+ "/lock_open.png")));
				layer.setLock(!layer.isLocked());
				layer.getParent().repaint();
			}
		}
	}

	private void createNameField()
	{
		labelPanel = new JPanel();
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.LINE_AXIS));
		label = new JLabel(layer.getName());
		labelPanel.addMouseListener(this);
		labelPanel.setBackground(Color.GRAY);
		labelPanel.add(Box.createRigidArea(new Dimension(3, 0)));
		labelPanel.add(label);
		labelPanel.setAlignmentY(CENTER_ALIGNMENT);
		labelPanel.setMinimumSize(new Dimension(120, 20));
		labelPanel.setMaximumSize(new Dimension(120, 20));
		labelPanel.setPreferredSize(new Dimension(120, 20));
		labelPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		add(Box.createRigidArea(new Dimension(38, 0)));
		add(labelPanel);
	}

	private void createModButtons()
	{
		JButton vis = new JButton((layer.isVisible() ? new ImageIcon(
				IconConstants.ICON_DIR + "/eye_open.png") : new ImageIcon(
				IconConstants.ICON_DIR + "/eye_close.png")));
		JButton lock = new JButton((layer.isLocked() ? new ImageIcon(
				IconConstants.ICON_DIR + "/lock_close.png") : new ImageIcon(
				IconConstants.ICON_DIR + "/lock_open.png")));

		vis.setActionCommand("Vis");
		lock.setActionCommand("Lock");

		initiateButton(vis);
		initiateButton(lock);

		add(Box.createRigidArea(new Dimension(35, 0)));
		add(vis);
		add(Box.createRigidArea(new Dimension(30, 0)));
		add(lock);
		add(Box.createRigidArea(new Dimension(30, 0)));
	}

	private void initiateButton(JButton button)
	{
		button.setAlignmentY(CENTER_ALIGNMENT);
		button.setOpaque(false);
		button.setMaximumSize(new Dimension(23, 23));
		button.setPreferredSize(new Dimension(23, 23));
		button.setMinimumSize(new Dimension(23, 23));
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.addActionListener(this);
	}

	private CollisionLayer layer;
	private static final Color BACKGROUND = Color.LIGHT_GRAY;
}
